package com.xdxc.excentionHandle;

import com.xdxc.exception.CustomException;
import com.xdxc.response.ApiResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 捕获自定义异常
     */
    @ExceptionHandler(CustomException.class)
    @ResponseStatus(HttpStatus.OK) // 返回 HTTP 200 状态码
    public ApiResponse<Void> handleCustomException(CustomException ex) {
        return ApiResponse.error(ex.getCode(), ex.getMessage());
    }

    /**
     * 捕获其他未处理的异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) // 返回 HTTP 500 状态码
    public ApiResponse<Void> handleGenericException(Exception ex) {
        return ApiResponse.error(500, "Internal Server Error: " + ex.getMessage());
    }
}